﻿using Vboot.Core.Module.Bpm;

namespace Vboot.Core.Module.Sys;

public class SysTodoMainService : BaseService<SysTodoMain>, ITransient
{
    public async Task SendTodo(Zbpm zbpm, Znode znode)
    {
        SysTodoMain todo = new SysTodoMain();
        todo.id = YitIdHelper.NextId() + "";
        todo.name = zbpm.prona;
        todo.link = "/#/page/ofmv?id=" + zbpm.proid;
        todo.modid = zbpm.proid;

        SysTodoUser todoTarget = new SysTodoUser();
        todoTarget.id = YitIdHelper.NextId() + "";
        todoTarget.todid = todo.id;
        todoTarget.useid = znode.exman;
        await repo.InsertAsync(todo);
        // repo.Context.Insertable(todoTarget).ExecuteCommand();
        await repo.Context.Insertable(todoTarget).ExecuteCommandAsync();
    }

    public async Task DoneTodo(Zbpm zbpm)
    {
        string sql = "select m.id,t.id as todid from sys_todo_main m inner join sys_todo_user t on t.todid=m.id " +
                     "where t.useid=@useid and m.modid=@modid";

        dynamic map = repo.Context.Ado.SqlQuerySingle<dynamic>(sql, new {useid = zbpm.haman, modid = zbpm.proid});
        if (map != null)
        {
            string todid = map.todid;
            await repo.Context.Deleteable<SysTodoUser>().Where(it => it.id == todid).ExecuteCommandAsync();
            SysTodoDone done = new SysTodoDone();
            done.id = YitIdHelper.NextId() + "";
            done.todid = "" + map.id;
            done.useid = zbpm.haman;
            done.entim = DateTime.Now;
            await repo.Context.Insertable(done).ExecuteCommandAsync();
        }
    }


    public SysTodoMainService(SqlSugarRepository<SysTodoMain> repo)
    {
        this.repo = repo;
    }
}